//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * packageinfoDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.packageinfo.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.packageinfo.dao.IPackageinfoDao;
import com.gamutsoft.amb.packageinfo.model.Packageinfo;

@SuppressWarnings("unchecked")
public class PackageinfoDaoHibernate extends GenericDaoHibernate<Packageinfo, Long> implements IPackageinfoDao {
    
     public PackageinfoDaoHibernate() {
		super(Packageinfo.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchPackageinfoList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.CODE,T.FREIGHT_CODE,T.POST_TYPE,T.PREDICT_DATE,T.ACTUAL_DATE,T.AMOUNT,T.FLOW_STATUS,T.CREATETIME,T.USER_ID,T.PAY_TYPE,T.PAY_STATUS,T.FREIGHT_ID,T.WEIGHT,T.CUBAGE,T.FREIGHT_AMOUNT,T.SAFETY_AMOUNT,T.SERVICE_AMOUNT,T.DISCOUNT_AMOUNT,T.ENTRY_AMOUNT,T.FREIGHT_AREA_ID FROM PACKAGEINFO T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public Packageinfo searchPackageinfoById(Long id) throws BusinessException {
	
	  Packageinfo domain = new Packageinfo();
	   String hql = "from Packageinfo where id=?";
	   List<Packageinfo> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deletePackageinfoById(Long id) throws BusinessException {
	
	 String hql ="delete from Packageinfo where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchPackageinfoListByWhere(QueryExample qe,
			Packageinfo domain) throws BusinessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.CODE,CP.FIRST_NAME,CP.LAST_NAME,T.FREIGHT_CODE,T.POST_TYPE,T.PREDICT_DATE,T.ACTUAL_DATE,T.AMOUNT,T.FLOW_STATUS,T.CREATETIME,T.USER_ID,T.PAY_TYPE,T.PAY_STATUS,T.FREIGHT_ID,T.WEIGHT,T.CUBAGE,T.FREIGHT_AMOUNT,T.SAFETY_AMOUNT,T.SERVICE_AMOUNT,T.DISCOUNT_AMOUNT,T.ENTRY_AMOUNT,T.FREIGHT_AREA_ID FROM PACKAGEINFO T");
		sb.append(" LEFT JOIN CONTACT_PERSON CP ON CP.PACKAGE_ID=T.ID ");
		if(domain!=null){
			if(domain.getFlowStatus()!=null && !"".equals(domain.getFlowStatus())){
					qe.addCondictions("T.FLOW_STATUS","=",domain.getFlowStatus());
			}
			if(domain.getPayStatus()!=null && !"".equals(domain.getPayStatus())){
				qe.addCondictions("T.PAY_STATUS","=",domain.getPayStatus());
		    }
			if(domain.getUserId()!=null){
				qe.addCondictions("T.USER_ID","=",domain.getUserId());
		    }
		}
		qe.addOrderBy(" T.CREATETIME DESC");
		return this.findByQueryExample(sb.toString(), qe);
	}

	@Override
	public List<Map> staticPackageinfo(QueryExample qe, Packageinfo domain)
			throws BusinessException {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT  'A'||T.FLOW_STATUS AS STATUS,COUNT(*) AS NUM FROM PACKAGEINFO T ");
		if(domain!=null){
			if(domain.getFlowStatus()!=null && !"".equals(domain.getFlowStatus())){
					qe.addCondictions("T.FLOW_STATUS","=",domain.getFlowStatus());
			}
			if(domain.getPayStatus()!=null && !"".equals(domain.getPayStatus())){
				qe.addCondictions("T.PAY_STATUS","=",domain.getPayStatus());
		    }
			if(domain.getUserId()!=null){
				qe.addCondictions("T.USER_ID","=",domain.getUserId());
		    }
		}
		qe.setGroupBy(" group by t.flow_status ");
		return null;
	}
	
}